3 namespace MediaWiki\Tests\Revision
;
5 use CommentStoreComment
;
9 use InvalidArgumentException
;
11 use MediaWiki\Linker\LinkTarget
;
12 use MediaWiki\MediaWikiServices
;
13 use MediaWiki\Revision\IncompleteRevisionException
;
14 use MediaWiki\Revision\MutableRevisionRecord
;
15 use MediaWiki\Revision\RevisionArchiveRecord
;
16 use MediaWiki\Revision\RevisionRecord
;
17 use MediaWiki\Revision\RevisionStoreRecord
;
18 use MediaWiki\Revision\RevisionSlots
;
19 use MediaWiki\Revision\RevisionStore
;
20 use MediaWiki\Revision\SlotRecord
;
21 use MediaWiki\Storage\BlobStoreFactory
;
22 use MediaWiki\Storage\SqlBlobStore
;
23 use MediaWiki\User\UserIdentityValue
;
24 use MediaWikiTestCase
;
25 use PHPUnit_Framework_MockObject_MockObject
;
26 use Psr\Log\NullLogger
;
31 use Wikimedia\Rdbms\Database
;
32 use Wikimedia\Rdbms\DatabaseDomain
;
33 use Wikimedia\Rdbms\DatabaseSqlite
;
34 use Wikimedia\Rdbms\FakeResultWrapper
;
35 use Wikimedia\Rdbms\LoadBalancer
;
36 use Wikimedia\Rdbms\TransactionProfiler
;
42 * @group RevisionStore
44 abstract class RevisionStoreDbTestBase
extends MediaWikiTestCase
{
49 private $testPageTitle;
59 abstract protected function getMcrMigrationStage();
64 protected function getContentHandlerUseDB() {
71 abstract protected function getMcrTablesToReset();
73 public function setUp() {
75 $this->tablesUsed
[] = 'archive';
76 $this->tablesUsed
[] = 'page';
77 $this->tablesUsed
[] = 'revision';
78 $this->tablesUsed
[] = 'comment';
80 $this->tablesUsed +
= $this->getMcrTablesToReset();
82 $this->setMwGlobals( [
83 'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
84 'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
85 'wgActorTableSchemaMigrationStage' => SCHEMA_COMPAT_NEW
,
88 $this->overrideMwServices();
91 protected function addCoreDBData() {
92 // Blank out. This would fail with a modified schema, and we don't need it.
98 protected function getTestPageTitle() {
99 if ( $this->testPageTitle
) {
100 return $this->testPageTitle
;
103 $this->testPageTitle
= Title
::newFromText( 'UTPage-' . __CLASS__
);
104 return $this->testPageTitle
;
110 protected function getTestPage() {
111 if ( $this->testPage
) {
112 return $this->testPage
;
115 $title = $this->getTestPageTitle();
116 $this->testPage
= WikiPage
::factory( $title );
118 if ( !$this->testPage
->exists() ) {
119 // Make sure we don't write to the live db.
120 $this->ensureMockDatabaseConnection( wfGetDB( DB_MASTER
) );
122 $user = static::getTestSysop()->getUser();
124 $this->testPage
->doEditContent(
125 new WikitextContent( 'UTContent-' . __CLASS__
),
126 'UTPageSummary-' . __CLASS__
,
127 EDIT_NEW | EDIT_SUPPRESS_RC
,
133 return $this->testPage
;
137 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
139 private function getLoadBalancerMock( array $server ) {
140 $domain = new DatabaseDomain( $server['dbname'], null, $server['tablePrefix'] );
142 $lb = $this->getMockBuilder( LoadBalancer
::class )
143 ->setMethods( [ 'reallyOpenConnection' ] )
144 ->setConstructorArgs( [
145 [ 'servers' => [ $server ], 'localDomain' => $domain ]
149 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
150 function () use ( $server ) {
151 return $this->getDatabaseMock( $server );
159 * @return Database|PHPUnit_Framework_MockObject_MockObject
161 private function getDatabaseMock( array $params ) {
162 $db = $this->getMockBuilder( DatabaseSqlite
::class )
163 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
164 ->setConstructorArgs( [ $params ] )
167 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
168 $db->method( 'isOpen' )->willReturn( true );
173 public function provideDomainCheck() {
174 yield
[ false, 'test', '' ];
175 yield
[ 'test', 'test', '' ];
177 yield
[ false, 'test', 'foo_' ];
178 yield
[ 'test-foo_', 'test', 'foo_' ];
180 yield
[ false, 'dash-test', '' ];
181 yield
[ 'dash-test', 'dash-test', '' ];
183 yield
[ false, 'underscore_test', 'foo_' ];
184 yield
[ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
188 * @dataProvider provideDomainCheck
189 * @covers \MediaWiki\Revision\RevisionStore::checkDatabaseDomain
191 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
194 'wgDBname' => $dbName,
195 'wgDBprefix' => $dbPrefix,
199 $loadBalancer = $this->getLoadBalancerMock(
202 'dbDirectory' => '*dummy*',
204 'password' => 'test',
211 'srvCache' => new HashBagOStuff(),
213 'trxProfiler' => new TransactionProfiler(),
214 'connLogger' => new NullLogger(),
215 'queryLogger' => new NullLogger(),
216 'errorLogger' => function () {
218 'deprecationLogger' => function () {
222 'tablePrefix' => $dbPrefix,
225 $db = $loadBalancer->getConnection( DB_REPLICA
);
227 /** @var SqlBlobStore $blobStore */
228 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
229 ->disableOriginalConstructor()
232 $store = new RevisionStore(
235 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
236 MediaWikiServices
::getInstance()->getCommentStore(),
237 MediaWikiServices
::getInstance()->getContentModelStore(),
238 MediaWikiServices
::getInstance()->getSlotRoleStore(),
239 MediaWikiServices
::getInstance()->getSlotRoleRegistry(),
240 $this->getMcrMigrationStage(),
241 MediaWikiServices
::getInstance()->getActorMigration(),
245 $count = $store->countRevisionsByPageId( $db, 0 );
247 // Dummy check to make PhpUnit happy. We are really only interested in
248 // countRevisionsByPageId not failing due to the DB domain check.
249 $this->assertSame( 0, $count );
252 protected function assertLinkTargetsEqual( LinkTarget
$l1, LinkTarget
$l2 ) {
253 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
254 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
255 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
256 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
259 protected function assertRevisionRecordsEqual( RevisionRecord
$r1, RevisionRecord
$r2 ) {
261 $r1->getPageAsLinkTarget()->getNamespace(),
262 $r2->getPageAsLinkTarget()->getNamespace()
266 $r1->getPageAsLinkTarget()->getText(),
267 $r2->getPageAsLinkTarget()->getText()
270 if ( $r1->getParentId() ) {
271 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
274 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
275 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
276 $this->assertEquals( $r1->getComment(), $r2->getComment() );
277 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
278 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
279 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
280 $this->assertEquals( $r1->getSize(), $r2->getSize() );
281 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
282 $this->assertArrayEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
283 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
284 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
285 foreach ( $r1->getSlotRoles() as $role ) {
286 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
287 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
290 RevisionRecord
::DELETED_TEXT
,
291 RevisionRecord
::DELETED_COMMENT
,
292 RevisionRecord
::DELETED_USER
,
293 RevisionRecord
::DELETED_RESTRICTED
,
295 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
299 protected function assertSlotRecordsEqual( SlotRecord
$s1, SlotRecord
$s2 ) {
300 $this->assertSame( $s1->getRole(), $s2->getRole() );
301 $this->assertSame( $s1->getModel(), $s2->getModel() );
302 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
303 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
304 $this->assertSame( $s1->getSize(), $s2->getSize() );
305 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
307 $s1->hasRevision() ?
$this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
308 $s1->hasAddress() ?
$this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
311 protected function assertRevisionCompleteness( RevisionRecord
$r ) {
312 $this->assertTrue( $r->hasSlot( SlotRecord
::MAIN
) );
313 $this->assertInstanceOf( SlotRecord
::class, $r->getSlot( SlotRecord
::MAIN
) );
314 $this->assertInstanceOf( Content
::class, $r->getContent( SlotRecord
::MAIN
) );
316 foreach ( $r->getSlotRoles() as $role ) {
317 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
321 protected function assertSlotCompleteness( RevisionRecord
$r, SlotRecord
$slot ) {
322 $this->assertTrue( $slot->hasAddress() );
323 $this->assertSame( $r->getId(), $slot->getRevision() );
325 $this->assertInstanceOf( Content
::class, $slot->getContent() );
329 * @param mixed[] $details
331 * @return RevisionRecord
333 private function getRevisionRecordFromDetailsArray( $details = [] ) {
334 // Convert some values that can't be provided by dataProviders
335 if ( isset( $details['user'] ) && $details['user'] === true ) {
336 $details['user'] = $this->getTestUser()->getUser();
338 if ( isset( $details['page'] ) && $details['page'] === true ) {
339 $details['page'] = $this->getTestPage()->getId();
341 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
342 $details['parent'] = $this->getTestPage()->getLatest();
345 // Create the RevisionRecord with any available data
346 $rev = new MutableRevisionRecord( $this->getTestPageTitle() );
347 isset( $details['slot'] ) ?
$rev->setSlot( $details['slot'] ) : null;
348 isset( $details['parent'] ) ?
$rev->setParentId( $details['parent'] ) : null;
349 isset( $details['page'] ) ?
$rev->setPageId( $details['page'] ) : null;
350 isset( $details['size'] ) ?
$rev->setSize( $details['size'] ) : null;
351 isset( $details['sha1'] ) ?
$rev->setSha1( $details['sha1'] ) : null;
352 isset( $details['comment'] ) ?
$rev->setComment( $details['comment'] ) : null;
353 isset( $details['timestamp'] ) ?
$rev->setTimestamp( $details['timestamp'] ) : null;
354 isset( $details['minor'] ) ?
$rev->setMinorEdit( $details['minor'] ) : null;
355 isset( $details['user'] ) ?
$rev->setUser( $details['user'] ) : null;
356 isset( $details['visibility'] ) ?
$rev->setVisibility( $details['visibility'] ) : null;
357 isset( $details['id'] ) ?
$rev->setId( $details['id'] ) : null;
359 if ( isset( $details['content'] ) ) {
360 foreach ( $details['content'] as $role => $content ) {
361 $rev->setContent( $role, $content );
368 public function provideInsertRevisionOn_successes() {
369 yield
'Bare minimum revision insertion' => [
371 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
373 'comment' => $this->getRandomCommentStoreComment(),
374 'timestamp' => '20171117010101',
378 yield
'Detailed revision insertion' => [
380 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
383 'comment' => $this->getRandomCommentStoreComment(),
384 'timestamp' => '20171117010101',
387 'visibility' => RevisionRecord
::DELETED_RESTRICTED
,
392 protected function getRandomCommentStoreComment() {
393 return CommentStoreComment
::newUnsavedComment( __METHOD__
. '.' . rand( 0, 1000 ) );
397 * @dataProvider provideInsertRevisionOn_successes
398 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
399 * @covers \MediaWiki\Revision\RevisionStore::insertSlotRowOn
400 * @covers \MediaWiki\Revision\RevisionStore::insertContentRowOn
402 public function testInsertRevisionOn_successes(
403 array $revDetails = []
405 $title = $this->getTestPageTitle();
406 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
408 $this->overrideMwServices();
409 $store = MediaWikiServices
::getInstance()->getRevisionStore();
410 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
412 // is the new revision correct?
413 $this->assertRevisionCompleteness( $return );
414 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
415 $this->assertRevisionRecordsEqual( $rev, $return );
417 // can we load it from the store?
418 $loaded = $store->getRevisionById( $return->getId() );
419 $this->assertRevisionCompleteness( $loaded );
420 $this->assertRevisionRecordsEqual( $return, $loaded );
422 // can we find it directly in the database?
423 $this->assertRevisionExistsInDatabase( $return );
426 protected function assertRevisionExistsInDatabase( RevisionRecord
$rev ) {
427 $row = $this->revisionToRow( new Revision( $rev ), [] );
429 // unset nulled fields
430 unset( $row->rev_content_model
);
431 unset( $row->rev_content_format
);
434 unset( $row->rev_comment_text
);
435 unset( $row->rev_comment_data
);
436 unset( $row->rev_comment_cid
);
437 unset( $row->rev_comment_id
);
439 $store = MediaWikiServices
::getInstance()->getRevisionStore();
440 $queryInfo = $store->getQueryInfo( [ 'user' ] );
442 $row = get_object_vars( $row );
444 // Use aliased fields from $queryInfo, e.g. rev_user
445 $keys = array_keys( $row );
446 $keys = array_combine( $keys, $keys );
447 $fields = array_intersect_key( $queryInfo['fields'], $keys ) +
$keys;
449 // assertSelect() fails unless the orders match.
454 $queryInfo['tables'],
456 [ 'rev_id' => $rev->getId() ],
457 [ array_values( $row ) ],
464 * @param SlotRecord $a
465 * @param SlotRecord $b
467 protected function assertSameSlotContent( SlotRecord
$a, SlotRecord
$b ) {
468 // Assert that the same blob address has been used.
469 $this->assertSame( $a->getAddress(), $b->getAddress() );
473 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
475 public function testInsertRevisionOn_blobAddressExists() {
476 $title = $this->getTestPageTitle();
478 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
480 'comment' => $this->getRandomCommentStoreComment(),
481 'timestamp' => '20171117010101',
485 $this->overrideMwServices();
486 $store = MediaWikiServices
::getInstance()->getRevisionStore();
488 // Insert the first revision
489 $revOne = $this->getRevisionRecordFromDetailsArray( $revDetails );
490 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER
) );
491 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
492 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
494 // Insert a second revision inheriting the same blob address
495 $revDetails['slot'] = SlotRecord
::newInherited( $firstReturn->getSlot( SlotRecord
::MAIN
) );
496 $revTwo = $this->getRevisionRecordFromDetailsArray( $revDetails );
497 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER
) );
498 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
499 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
501 $firstMainSlot = $firstReturn->getSlot( SlotRecord
::MAIN
);
502 $secondMainSlot = $secondReturn->getSlot( SlotRecord
::MAIN
);
504 $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
506 // And that different revisions have been created.
507 $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
509 // Make sure the slot rows reference the correct revision
510 $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
511 $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
514 public function provideInsertRevisionOn_failures() {
517 'comment' => $this->getRandomCommentStoreComment(),
518 'timestamp' => '20171117010101',
521 new InvalidArgumentException( 'main slot must be provided' )
523 yield
'no main slot' => [
525 'slot' => SlotRecord
::newUnsaved( 'aux', new WikitextContent( 'Turkey' ) ),
526 'comment' => $this->getRandomCommentStoreComment(),
527 'timestamp' => '20171117010101',
530 new InvalidArgumentException( 'main slot must be provided' )
532 yield
'no timestamp' => [
534 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
535 'comment' => $this->getRandomCommentStoreComment(),
538 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
540 yield
'no comment' => [
542 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
543 'timestamp' => '20171117010101',
546 new IncompleteRevisionException( 'comment must not be NULL!' )
550 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
551 'comment' => $this->getRandomCommentStoreComment(),
552 'timestamp' => '20171117010101',
554 new IncompleteRevisionException( 'user must not be NULL!' )
559 * @dataProvider provideInsertRevisionOn_failures
560 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
562 public function testInsertRevisionOn_failures(
563 array $revDetails = [],
566 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
568 $store = MediaWikiServices
::getInstance()->getRevisionStore();
570 $this->setExpectedException(
571 get_class( $exception ),
572 $exception->getMessage(),
573 $exception->getCode()
575 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
578 public function provideNewNullRevision() {
580 Title
::newFromText( 'UTPage_notAutoCreated' ),
581 [ 'content' => [ 'main' => new WikitextContent( 'Flubber1' ) ] ],
582 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment1' ),
586 Title
::newFromText( 'UTPage_notAutoCreated' ),
587 [ 'content' => [ 'main' => new WikitextContent( 'Flubber2' ) ] ],
588 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment2', [ 'a' => 1 ] ),
594 * @dataProvider provideNewNullRevision
595 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
596 * @covers \MediaWiki\Revision\RevisionStore::findSlotContentId
598 public function testNewNullRevision( Title
$title, $revDetails, $comment, $minor = false ) {
599 $this->overrideMwServices();
601 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
602 $page = WikiPage
::factory( $title );
604 if ( !$page->exists() ) {
605 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
, EDIT_NEW
);
608 $revDetails['page'] = $page->getId();
609 $revDetails['timestamp'] = wfTimestampNow();
610 $revDetails['comment'] = CommentStoreComment
::newUnsavedComment( 'Base' );
611 $revDetails['user'] = $user;
613 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
614 $store = MediaWikiServices
::getInstance()->getRevisionStore();
616 $dbw = wfGetDB( DB_MASTER
);
617 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
618 $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
620 $record = $store->newNullRevision(
621 wfGetDB( DB_MASTER
),
628 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
629 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
630 $this->assertEquals( $comment, $record->getComment() );
631 $this->assertEquals( $minor, $record->isMinor() );
632 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
633 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
635 $this->assertArrayEquals(
636 $baseRev->getSlotRoles(),
637 $record->getSlotRoles()
640 foreach ( $baseRev->getSlotRoles() as $role ) {
641 $parentSlot = $baseRev->getSlot( $role );
642 $slot = $record->getSlot( $role );
644 $this->assertTrue( $slot->isInherited(), 'isInherited' );
645 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
646 $this->assertSameSlotContent( $parentSlot, $slot );
651 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
653 public function testNewNullRevision_nonExistingTitle() {
654 $store = MediaWikiServices
::getInstance()->getRevisionStore();
655 $record = $store->newNullRevision(
656 wfGetDB( DB_MASTER
),
657 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
658 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
660 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
662 $this->assertNull( $record );
666 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
668 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
669 $page = $this->getTestPage();
670 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
671 /** @var Revision $rev */
672 $rev = $status->value
['revision'];
674 $store = MediaWikiServices
::getInstance()->getRevisionStore();
675 $revisionRecord = $store->getRevisionById( $rev->getId() );
676 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
678 $this->assertGreaterThan( 0, $result );
680 $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
686 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
688 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
689 // This assumes that sysops are auto patrolled
690 $sysop = $this->getTestSysop()->getUser();
691 $page = $this->getTestPage();
692 $status = $page->doEditContent(
693 new WikitextContent( __METHOD__
),
699 /** @var Revision $rev */
700 $rev = $status->value
['revision'];
702 $store = MediaWikiServices
::getInstance()->getRevisionStore();
703 $revisionRecord = $store->getRevisionById( $rev->getId() );
704 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
706 $this->assertSame( 0, $result );
710 * @covers \MediaWiki\Revision\RevisionStore::getRecentChange
712 public function testGetRecentChange() {
713 $page = $this->getTestPage();
714 $content = new WikitextContent( __METHOD__
);
715 $status = $page->doEditContent( $content, __METHOD__
);
716 /** @var Revision $rev */
717 $rev = $status->value
['revision'];
719 $store = MediaWikiServices
::getInstance()->getRevisionStore();
720 $revRecord = $store->getRevisionById( $rev->getId() );
721 $recentChange = $store->getRecentChange( $revRecord );
723 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
724 $this->assertEquals( $rev->getRecentChange(), $recentChange );
728 * @covers \MediaWiki\Revision\RevisionStore::getRevisionById
730 public function testGetRevisionById() {
731 $page = $this->getTestPage();
732 $content = new WikitextContent( __METHOD__
);
733 $status = $page->doEditContent( $content, __METHOD__
);
734 /** @var Revision $rev */
735 $rev = $status->value
['revision'];
737 $store = MediaWikiServices
::getInstance()->getRevisionStore();
738 $revRecord = $store->getRevisionById( $rev->getId() );
740 $this->assertSame( $rev->getId(), $revRecord->getId() );
741 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
742 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
746 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTitle
748 public function testGetRevisionByTitle() {
749 $page = $this->getTestPage();
750 $content = new WikitextContent( __METHOD__
);
751 $status = $page->doEditContent( $content, __METHOD__
);
752 /** @var Revision $rev */
753 $rev = $status->value
['revision'];
755 $store = MediaWikiServices
::getInstance()->getRevisionStore();
756 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
758 $this->assertSame( $rev->getId(), $revRecord->getId() );
759 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
760 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
764 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByPageId
766 public function testGetRevisionByPageId() {
767 $page = $this->getTestPage();
768 $content = new WikitextContent( __METHOD__
);
769 $status = $page->doEditContent( $content, __METHOD__
);
770 /** @var Revision $rev */
771 $rev = $status->value
['revision'];
773 $store = MediaWikiServices
::getInstance()->getRevisionStore();
774 $revRecord = $store->getRevisionByPageId( $page->getId() );
776 $this->assertSame( $rev->getId(), $revRecord->getId() );
777 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
778 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
782 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTimestamp
784 public function testGetRevisionByTimestamp() {
785 // Make sure there is 1 second between the last revision and the rev we create...
786 // Otherwise we might not get the correct revision and the test may fail...
788 $page = $this->getTestPage();
790 $content = new WikitextContent( __METHOD__
);
791 $status = $page->doEditContent( $content, __METHOD__
);
792 /** @var Revision $rev */
793 $rev = $status->value
['revision'];
795 $store = MediaWikiServices
::getInstance()->getRevisionStore();
796 $revRecord = $store->getRevisionByTimestamp(
801 $this->assertSame( $rev->getId(), $revRecord->getId() );
802 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
803 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
806 protected function revisionToRow( Revision
$rev, $options = [ 'page', 'user', 'comment' ] ) {
807 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
808 $page = WikiPage
::factory( $rev->getTitle() );
811 'rev_id' => (string)$rev->getId(),
812 'rev_page' => (string)$rev->getPage(),
813 'rev_timestamp' => $this->db
->timestamp( $rev->getTimestamp() ),
814 'rev_user_text' => (string)$rev->getUserText(),
815 'rev_user' => (string)$rev->getUser() ?
: null,
816 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
817 'rev_deleted' => (string)$rev->getVisibility(),
818 'rev_len' => (string)$rev->getSize(),
819 'rev_parent_id' => (string)$rev->getParentId(),
820 'rev_sha1' => (string)$rev->getSha1(),
823 if ( in_array( 'page', $options ) ) {
825 'page_namespace' => (string)$page->getTitle()->getNamespace(),
826 'page_title' => $page->getTitle()->getDBkey(),
827 'page_id' => (string)$page->getId(),
828 'page_latest' => (string)$page->getLatest(),
829 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
830 'page_len' => (string)$page->getContent()->getSize(),
834 if ( in_array( 'user', $options ) ) {
836 'user_name' => (string)$rev->getUserText(),
840 if ( in_array( 'comment', $options ) ) {
842 'rev_comment_text' => $rev->getComment(),
843 'rev_comment_data' => null,
844 'rev_comment_cid' => null,
848 if ( $rev->getId() ) {
850 'rev_id' => (string)$rev->getId(),
854 return (object)$fields;
857 protected function assertRevisionRecordMatchesRevision(
859 RevisionRecord
$record
861 $this->assertSame( $rev->getId(), $record->getId() );
862 $this->assertSame( $rev->getPage(), $record->getPageId() );
863 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
864 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
865 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
866 $this->assertSame( $rev->isMinor(), $record->isMinor() );
867 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
868 $this->assertSame( $rev->getSize(), $record->getSize() );
870 * @note As of MW 1.31, the database schema allows the parent ID to be
871 * NULL to indicate that it is unknown.
873 $expectedParent = $rev->getParentId();
874 if ( $expectedParent === null ) {
877 $this->assertSame( $expectedParent, $record->getParentId() );
878 $this->assertSame( $rev->getSha1(), $record->getSha1() );
879 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
880 $this->assertSame( $rev->getContentFormat(),
881 $record->getContent( SlotRecord
::MAIN
)->getDefaultFormat() );
882 $this->assertSame( $rev->getContentModel(), $record->getContent( SlotRecord
::MAIN
)->getModel() );
883 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
885 $revRec = $rev->getRevisionRecord();
886 $revMain = $revRec->getSlot( SlotRecord
::MAIN
);
887 $recMain = $record->getSlot( SlotRecord
::MAIN
);
889 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
890 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
891 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
893 if ( $revMain->hasOrigin() ) {
894 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
897 if ( $revMain->hasAddress() ) {
898 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
901 if ( $revMain->hasContentId() ) {
902 // XXX: the content ID value is ill-defined when SCHEMA_COMPAT_WRITE_BOTH and
903 // SCHEMA_COMPAT_READ_OLD is set, since revision insertion will report the
904 // content ID used with the new schema, while loading the revision from the
905 // old schema will report an emulated ID.
906 if ( $this->getMcrMigrationStage() & SCHEMA_COMPAT_READ_NEW
) {
907 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
913 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
914 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
916 public function testNewRevisionFromRowAndSlot_getQueryInfo() {
917 $page = $this->getTestPage();
918 $text = __METHOD__
. 'o-ö';
919 /** @var Revision $rev */
920 $rev = $page->doEditContent(
921 new WikitextContent( $text ),
923 )->value
['revision'];
925 $store = MediaWikiServices
::getInstance()->getRevisionStore();
926 $info = $store->getQueryInfo();
927 $row = $this->db
->selectRow(
930 [ 'rev_id' => $rev->getId() ],
936 $info = $store->getSlotsQueryInfo( [ 'content' ] );
937 $slotRows = $this->db
->select(
940 $this->getSlotRevisionConditions( $rev->getId() ),
946 $record = $store->newRevisionFromRowAndSlots(
948 iterator_to_array( $slotRows ),
952 $this->assertRevisionRecordMatchesRevision( $rev, $record );
953 $this->assertSame( $text, $rev->getContent()->serialize() );
957 * Conditions to use together with getSlotsQueryInfo() when selecting slot rows for a given
962 abstract protected function getSlotRevisionConditions( $revId );
965 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
966 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
967 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
969 public function testNewRevisionFromRow_getQueryInfo() {
970 $page = $this->getTestPage();
971 $text = __METHOD__
. 'a-ä';
972 /** @var Revision $rev */
973 $rev = $page->doEditContent(
974 new WikitextContent( $text ),
976 )->value
['revision'];
978 $store = MediaWikiServices
::getInstance()->getRevisionStore();
979 $info = $store->getQueryInfo();
980 $row = $this->db
->selectRow(
983 [ 'rev_id' => $rev->getId() ],
988 $record = $store->newRevisionFromRow(
993 $this->assertRevisionRecordMatchesRevision( $rev, $record );
994 $this->assertSame( $text, $rev->getContent()->serialize() );
998 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
999 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
1001 public function testNewRevisionFromRow_anonEdit() {
1002 $page = $this->getTestPage();
1003 $text = __METHOD__
. 'a-ä';
1004 /** @var Revision $rev */
1005 $rev = $page->doEditContent(
1006 new WikitextContent( $text ),
1008 )->value
['revision'];
1010 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1011 $record = $store->newRevisionFromRow(
1012 $this->revisionToRow( $rev ),
1016 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1017 $this->assertSame( $text, $rev->getContent()->serialize() );
1021 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1022 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
1024 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
1025 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1026 $this->overrideMwServices();
1027 $page = $this->getTestPage();
1028 $text = __METHOD__
. 'a-ä';
1029 /** @var Revision $rev */
1030 $rev = $page->doEditContent(
1031 new WikitextContent( $text ),
1033 )->value
['revision'];
1035 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1036 $record = $store->newRevisionFromRow(
1037 $this->revisionToRow( $rev ),
1041 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1042 $this->assertSame( $text, $rev->getContent()->serialize() );
1046 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1047 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
1049 public function testNewRevisionFromRow_userEdit() {
1050 $page = $this->getTestPage();
1051 $text = __METHOD__
. 'b-ä';
1052 /** @var Revision $rev */
1053 $rev = $page->doEditContent(
1054 new WikitextContent( $text ),
1058 $this->getTestUser()->getUser()
1059 )->value
['revision'];
1061 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1062 $record = $store->newRevisionFromRow(
1063 $this->revisionToRow( $rev ),
1067 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1068 $this->assertSame( $text, $rev->getContent()->serialize() );
1072 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1073 * @covers \MediaWiki\Revision\RevisionStore::getArchiveQueryInfo
1075 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
1076 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1077 $title = Title
::newFromText( __METHOD__
);
1078 $text = __METHOD__
. '-bä';
1079 $page = WikiPage
::factory( $title );
1080 /** @var Revision $orig */
1081 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1082 ->value
['revision'];
1083 $page->doDeleteArticle( __METHOD__
);
1085 $db = wfGetDB( DB_MASTER
);
1086 $arQuery = $store->getArchiveQueryInfo();
1088 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1089 __METHOD__
, [], $arQuery['joins']
1091 $this->assertTrue( is_object( $res ), 'query failed' );
1093 $row = $res->fetchObject();
1095 $record = $store->newRevisionFromArchiveRow( $row );
1097 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1098 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1102 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1104 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1105 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1106 $this->overrideMwServices();
1107 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1108 $title = Title
::newFromText( __METHOD__
);
1109 $text = __METHOD__
. '-bä';
1110 $page = WikiPage
::factory( $title );
1111 /** @var Revision $orig */
1112 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1113 ->value
['revision'];
1114 $page->doDeleteArticle( __METHOD__
);
1116 $db = wfGetDB( DB_MASTER
);
1117 $arQuery = $store->getArchiveQueryInfo();
1119 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1120 __METHOD__
, [], $arQuery['joins']
1122 $this->assertTrue( is_object( $res ), 'query failed' );
1124 $row = $res->fetchObject();
1126 $record = $store->newRevisionFromArchiveRow( $row );
1128 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1129 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1133 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1135 public function testNewRevisionFromArchiveRow_no_user() {
1136 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1140 'ar_page_id' => '2',
1141 'ar_namespace' => '0',
1142 'ar_title' => 'Something',
1144 'ar_text_id' => '47',
1145 'ar_timestamp' => '20180528192356',
1146 'ar_minor_edit' => '0',
1147 'ar_deleted' => '0',
1149 'ar_parent_id' => '0',
1150 'ar_sha1' => 'deadbeef',
1151 'ar_comment_text' => 'whatever',
1152 'ar_comment_data' => null,
1153 'ar_comment_cid' => null,
1155 'ar_user_text' => '', // this is the important bit
1157 'ar_content_format' => null,
1158 'ar_content_model' => null,
1161 \Wikimedia\
suppressWarnings();
1162 $record = $store->newRevisionFromArchiveRow( $row );
1163 \Wikimedia\
suppressWarnings( true );
1165 $this->assertInstanceOf( RevisionRecord
::class, $record );
1166 $this->assertInstanceOf( UserIdentityValue
::class, $record->getUser() );
1167 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1171 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1172 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
1174 public function testNewRevisionFromRow_no_user() {
1175 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1176 $title = Title
::newFromText( __METHOD__
);
1181 'page_namespace' => '0',
1182 'page_title' => $title->getText(),
1183 'rev_text_id' => '47',
1184 'rev_timestamp' => '20180528192356',
1185 'rev_minor_edit' => '0',
1186 'rev_deleted' => '0',
1188 'rev_parent_id' => '0',
1189 'rev_sha1' => 'deadbeef',
1190 'rev_comment_text' => 'whatever',
1191 'rev_comment_data' => null,
1192 'rev_comment_cid' => null,
1194 'rev_user_text' => '', // this is the important bit
1195 'rev_actor' => null,
1196 'rev_content_format' => null,
1197 'rev_content_model' => null,
1200 \Wikimedia\
suppressWarnings();
1201 $record = $store->newRevisionFromRow( $row, 0, $title );
1202 \Wikimedia\
suppressWarnings( true );
1204 $this->assertNotNull( $record );
1205 $this->assertNotNull( $record->getUser() );
1206 $this->assertNotEmpty( $record->getUser()->getName() );
1210 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
1212 public function testInsertRevisionOn_archive() {
1213 // This is a round trip test for deletion and undeletion of a
1214 // revision row via the archive table.
1216 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1217 $title = Title
::newFromText( __METHOD__
);
1219 $page = WikiPage
::factory( $title );
1220 /** @var Revision $origRev */
1221 $page->doEditContent( new WikitextContent( "First" ), __METHOD__
. '-first' );
1222 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__
)
1223 ->value
['revision'];
1224 $orig = $origRev->getRevisionRecord();
1225 $page->doDeleteArticle( __METHOD__
);
1227 // re-create page, so we can later load revisions for it
1228 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__
);
1230 $db = wfGetDB( DB_MASTER
);
1231 $arQuery = $store->getArchiveQueryInfo();
1232 $row = $db->selectRow(
1233 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1234 __METHOD__
, [], $arQuery['joins']
1237 $this->assertNotFalse( $row, 'query failed' );
1239 $record = $store->newRevisionFromArchiveRow(
1243 [ 'page_id' => $title->getArticleID() ]
1246 $restored = $store->insertRevisionOn( $record, $db );
1248 // is the new revision correct?
1249 $this->assertRevisionCompleteness( $restored );
1250 $this->assertRevisionRecordsEqual( $record, $restored );
1252 // does the new revision use the original slot?
1253 $recMain = $record->getSlot( SlotRecord
::MAIN
);
1254 $restMain = $restored->getSlot( SlotRecord
::MAIN
);
1255 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1256 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1257 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1258 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1260 // can we load it from the store?
1261 $loaded = $store->getRevisionById( $restored->getId() );
1262 $this->assertNotNull( $loaded );
1263 $this->assertRevisionCompleteness( $loaded );
1264 $this->assertRevisionRecordsEqual( $restored, $loaded );
1266 // can we find it directly in the database?
1267 $this->assertRevisionExistsInDatabase( $restored );
1271 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromId
1273 public function testLoadRevisionFromId() {
1274 $title = Title
::newFromText( __METHOD__
);
1275 $page = WikiPage
::factory( $title );
1276 /** @var Revision $rev */
1277 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1278 ->value
['revision'];
1280 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1281 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
1282 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1286 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromPageId
1288 public function testLoadRevisionFromPageId() {
1289 $title = Title
::newFromText( __METHOD__
);
1290 $page = WikiPage
::factory( $title );
1291 /** @var Revision $rev */
1292 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1293 ->value
['revision'];
1295 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1296 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
1297 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1301 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTitle
1303 public function testLoadRevisionFromTitle() {
1304 $title = Title
::newFromText( __METHOD__
);
1305 $page = WikiPage
::factory( $title );
1306 /** @var Revision $rev */
1307 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1308 ->value
['revision'];
1310 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1311 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
1312 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1316 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTimestamp
1318 public function testLoadRevisionFromTimestamp() {
1319 $title = Title
::newFromText( __METHOD__
);
1320 $page = WikiPage
::factory( $title );
1321 /** @var Revision $revOne */
1322 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1323 ->value
['revision'];
1324 // Sleep to ensure different timestamps... )(evil)
1326 /** @var Revision $revTwo */
1327 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
1328 ->value
['revision'];
1330 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1332 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
1336 $store->loadRevisionFromTimestamp(
1337 wfGetDB( DB_MASTER
),
1339 $revOne->getTimestamp()
1344 $store->loadRevisionFromTimestamp(
1345 wfGetDB( DB_MASTER
),
1347 $revTwo->getTimestamp()
1353 * @covers \MediaWiki\Revision\RevisionStore::listRevisionSizes
1355 public function testGetParentLengths() {
1356 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1357 /** @var Revision $revOne */
1358 $revOne = $page->doEditContent(
1359 new WikitextContent( __METHOD__
), __METHOD__
1360 )->value
['revision'];
1361 /** @var Revision $revTwo */
1362 $revTwo = $page->doEditContent(
1363 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1364 )->value
['revision'];
1366 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1369 $revOne->getId() => strlen( __METHOD__
),
1371 $store->listRevisionSizes(
1372 wfGetDB( DB_MASTER
),
1373 [ $revOne->getId() ]
1378 $revOne->getId() => strlen( __METHOD__
),
1379 $revTwo->getId() => strlen( __METHOD__
) +
1,
1381 $store->listRevisionSizes(
1382 wfGetDB( DB_MASTER
),
1383 [ $revOne->getId(), $revTwo->getId() ]
1389 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1391 public function testGetPreviousRevision() {
1392 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1393 /** @var Revision $revOne */
1394 $revOne = $page->doEditContent(
1395 new WikitextContent( __METHOD__
), __METHOD__
1396 )->value
['revision'];
1397 /** @var Revision $revTwo */
1398 $revTwo = $page->doEditContent(
1399 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1400 )->value
['revision'];
1402 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1404 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1408 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1413 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1415 public function testGetNextRevision() {
1416 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1417 /** @var Revision $revOne */
1418 $revOne = $page->doEditContent(
1419 new WikitextContent( __METHOD__
), __METHOD__
1420 )->value
['revision'];
1421 /** @var Revision $revTwo */
1422 $revTwo = $page->doEditContent(
1423 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1424 )->value
['revision'];
1426 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1429 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1432 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1436 public function provideNonHistoryRevision() {
1437 $title = Title
::newFromText( __METHOD__
);
1438 $rev = new MutableRevisionRecord( $title );
1441 $user = new UserIdentityValue( 7, 'Frank', 0 );
1442 $comment = CommentStoreComment
::newUnsavedComment( 'Test' );
1445 'ar_rev_id' => 34567,
1448 'ar_minor_edit' => 0,
1449 'ar_timestamp' => '20180101020202',
1451 $slots = new RevisionSlots( [] );
1452 $rev = new RevisionArchiveRecord( $title, $user, $comment, $row, $slots );
1457 * @dataProvider provideNonHistoryRevision
1458 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1460 public function testGetPreviousRevision_bad( RevisionRecord
$rev ) {
1461 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1462 $this->assertNull( $store->getPreviousRevision( $rev ) );
1466 * @dataProvider provideNonHistoryRevision
1467 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1469 public function testGetNextRevision_bad( RevisionRecord
$rev ) {
1470 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1471 $this->assertNull( $store->getNextRevision( $rev ) );
1475 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1477 public function testGetTimestampFromId_found() {
1478 $page = $this->getTestPage();
1479 /** @var Revision $rev */
1480 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1481 ->value
['revision'];
1483 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1484 $result = $store->getTimestampFromId( $rev->getId() );
1486 $this->assertSame( $rev->getTimestamp(), $result );
1490 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1492 public function testGetTimestampFromId_notFound() {
1493 $page = $this->getTestPage();
1494 /** @var Revision $rev */
1495 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1496 ->value
['revision'];
1498 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1499 $result = $store->getTimestampFromId( $rev->getId() +
1 );
1501 $this->assertFalse( $result );
1505 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByPageId
1507 public function testCountRevisionsByPageId() {
1508 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1509 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1513 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1515 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1518 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1520 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1523 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1528 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByTitle
1530 public function testCountRevisionsByTitle() {
1531 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1532 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1536 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1538 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1541 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1543 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1546 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1551 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1553 public function testUserWasLastToEdit_false() {
1554 $sysop = $this->getTestSysop()->getUser();
1555 $page = $this->getTestPage();
1556 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1558 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1559 $result = $store->userWasLastToEdit(
1560 wfGetDB( DB_MASTER
),
1565 $this->assertFalse( $result );
1569 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1571 public function testUserWasLastToEdit_true() {
1572 $startTime = wfTimestampNow();
1573 $sysop = $this->getTestSysop()->getUser();
1574 $page = $this->getTestPage();
1575 $page->doEditContent(
1576 new WikitextContent( __METHOD__
),
1583 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1584 $result = $store->userWasLastToEdit(
1585 wfGetDB( DB_MASTER
),
1590 $this->assertTrue( $result );
1594 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1596 public function testGetKnownCurrentRevision() {
1597 $page = $this->getTestPage();
1598 /** @var Revision $rev */
1599 $rev = $page->doEditContent(
1600 new WikitextContent( __METHOD__
. 'b' ),
1604 $this->getTestUser()->getUser()
1605 )->value
['revision'];
1607 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1608 $record = $store->getKnownCurrentRevision(
1613 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1616 public function provideNewMutableRevisionFromArray() {
1617 yield
'Basic array, content object' => [
1621 'timestamp' => '20171017114835',
1622 'user_text' => '111.0.1.2',
1624 'minor_edit' => false,
1628 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1629 'comment' => 'Goat Comment!',
1630 'content' => new WikitextContent( 'Some Content' ),
1633 yield
'Basic array, serialized text' => [
1637 'timestamp' => '20171017114835',
1638 'user_text' => '111.0.1.2',
1640 'minor_edit' => false,
1644 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1645 'comment' => 'Goat Comment!',
1646 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1649 yield
'Basic array, serialized text, utf-8 flags' => [
1653 'timestamp' => '20171017114835',
1654 'user_text' => '111.0.1.2',
1656 'minor_edit' => false,
1660 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1661 'comment' => 'Goat Comment!',
1662 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1666 yield
'Basic array, with title' => [
1668 'title' => Title
::newFromText( 'SomeText' ),
1669 'timestamp' => '20171017114835',
1670 'user_text' => '111.0.1.2',
1672 'minor_edit' => false,
1676 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1677 'comment' => 'Goat Comment!',
1678 'content' => new WikitextContent( 'Some Content' ),
1681 yield
'Basic array, no user field' => [
1685 'timestamp' => '20171017114835',
1686 'user_text' => '111.0.1.3',
1687 'minor_edit' => false,
1691 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1692 'comment' => 'Goat Comment!',
1693 'content' => new WikitextContent( 'Some Content' ),
1699 * @dataProvider provideNewMutableRevisionFromArray
1700 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1702 public function testNewMutableRevisionFromArray( array $array ) {
1703 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1705 // HACK: if $array['page'] is given, make sure that the page exists
1706 if ( isset( $array['page'] ) ) {
1707 $t = Title
::newFromID( $array['page'] );
1708 if ( !$t ||
!$t->exists() ) {
1709 $t = Title
::makeTitle( NS_MAIN
, __METHOD__
);
1710 $info = $this->insertPage( $t );
1711 $array['page'] = $info['id'];
1715 $result = $store->newMutableRevisionFromArray( $array );
1717 if ( isset( $array['id'] ) ) {
1718 $this->assertSame( $array['id'], $result->getId() );
1720 if ( isset( $array['page'] ) ) {
1721 $this->assertSame( $array['page'], $result->getPageId() );
1723 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1724 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1725 if ( isset( $array['user'] ) ) {
1726 $this->assertSame( $array['user'], $result->getUser()->getId() );
1728 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1729 $this->assertSame( $array['deleted'], $result->getVisibility() );
1730 $this->assertSame( $array['len'], $result->getSize() );
1731 $this->assertSame( $array['parent_id'], $result->getParentId() );
1732 $this->assertSame( $array['sha1'], $result->getSha1() );
1733 $this->assertSame( $array['comment'], $result->getComment()->text
);
1734 if ( isset( $array['content'] ) ) {
1735 foreach ( $array['content'] as $role => $content ) {
1737 $result->getContent( $role )->equals( $content )
1740 } elseif ( isset( $array['text'] ) ) {
1741 $this->assertSame( $array['text'],
1742 $result->getSlot( SlotRecord
::MAIN
)->getContent()->serialize() );
1743 } elseif ( isset( $array['content_format'] ) ) {
1745 $array['content_format'],
1746 $result->getSlot( SlotRecord
::MAIN
)->getContent()->getDefaultFormat()
1748 $this->assertSame( $array['content_model'], $result->getSlot( SlotRecord
::MAIN
)->getModel() );
1753 * @dataProvider provideNewMutableRevisionFromArray
1754 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1756 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1757 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1758 $services = MediaWikiServices
::getInstance();
1759 $lb = $services->getDBLoadBalancer();
1760 $access = $services->getExternalStoreAccess();
1761 $blobStore = new SqlBlobStore( $lb, $access, $cache );
1762 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1764 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1765 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1766 ->disableOriginalConstructor()
1768 $factory->expects( $this->any() )
1769 ->method( 'newBlobStore' )
1770 ->willReturn( $blobStore );
1771 $factory->expects( $this->any() )
1772 ->method( 'newSqlBlobStore' )
1773 ->willReturn( $blobStore );
1775 $this->setService( 'BlobStoreFactory', $factory );
1777 $this->testNewMutableRevisionFromArray( $array );
1781 * Creates a new revision for testing caching behavior
1783 * @param WikiPage $page the page for the new revision
1784 * @param RevisionStore $store store object to use for creating the revision
1785 * @return bool|RevisionStoreRecord the revision created, or false if missing
1787 private function createRevisionStoreCacheRecord( $page, $store ) {
1788 $user = MediaWikiTestCase
::getMutableTestUser()->getUser();
1789 $updater = $page->newPageUpdater( $user );
1790 $updater->setContent( SlotRecord
::MAIN
, new WikitextContent( __METHOD__
) );
1791 $summary = CommentStoreComment
::newUnsavedComment( __METHOD__
);
1792 $rev = $updater->saveRevision( $summary, EDIT_NEW
);
1793 return $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1797 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1799 public function testGetKnownCurrentRevision_userNameChange() {
1800 global $wgActorTableSchemaMigrationStage;
1802 $this->overrideMwServices();
1803 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1804 $this->setService( 'MainWANObjectCache', $cache );
1806 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1807 $page = $this->getNonexistingTestPage();
1808 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1810 // Grab the user name
1811 $userNameBefore = $rev->getUser()->getName();
1813 // Change the user name in the database, "behind the back" of the cache
1814 $newUserName = "Renamed $userNameBefore";
1815 $this->db
->update( 'revision',
1816 [ 'rev_user_text' => $newUserName ],
1817 [ 'rev_id' => $rev->getId() ] );
1818 $this->db
->update( 'user',
1819 [ 'user_name' => $newUserName ],
1820 [ 'user_id' => $rev->getUser()->getId() ] );
1821 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) {
1822 $this->db
->update( 'actor',
1823 [ 'actor_name' => $newUserName ],
1824 [ 'actor_user' => $rev->getUser()->getId() ] );
1827 // Reload the revision and regrab the user name.
1828 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1829 $userNameAfter = $revAfter->getUser()->getName();
1831 // The two user names should be different.
1832 // If they are the same, we are seeing a cached value, which is bad.
1833 $this->assertNotSame( $userNameBefore, $userNameAfter );
1835 // This is implied by the above assertion, but explicitly check it, for completeness
1836 $this->assertSame( $newUserName, $userNameAfter );
1840 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1842 public function testGetKnownCurrentRevision_revDelete() {
1843 $this->overrideMwServices();
1844 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1845 $this->setService( 'MainWANObjectCache', $cache );
1847 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1848 $page = $this->getNonexistingTestPage();
1849 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1851 // Grab the deleted bitmask
1852 $deletedBefore = $rev->getVisibility();
1854 // Change the deleted bitmask in the database, "behind the back" of the cache
1855 $this->db
->update( 'revision',
1856 [ 'rev_deleted' => RevisionRecord
::DELETED_TEXT
],
1857 [ 'rev_id' => $rev->getId() ] );
1859 // Reload the revision and regrab the visibility flag.
1860 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1861 $deletedAfter = $revAfter->getVisibility();
1863 // The two deleted flags should be different.
1864 // If they are the same, we are seeing a cached value, which is bad.
1865 $this->assertNotSame( $deletedBefore, $deletedAfter );
1867 // This is implied by the above assertion, but explicitly check it, for completeness
1868 $this->assertSame( RevisionRecord
::DELETED_TEXT
, $deletedAfter );
1872 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1874 public function testNewRevisionFromRow_userNameChange() {
1875 global $wgActorTableSchemaMigrationStage;
1877 $page = $this->getTestPage();
1879 /** @var Revision $rev */
1880 $rev = $page->doEditContent(
1881 new WikitextContent( $text ),
1885 $this->getMutableTestUser()->getUser()
1886 )->value
['revision'];
1888 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1889 $record = $store->newRevisionFromRow(
1890 $this->revisionToRow( $rev ),
1895 // Grab the user name
1896 $userNameBefore = $record->getUser()->getName();
1898 // Change the user name in the database
1899 $newUserName = "Renamed $userNameBefore";
1900 $this->db
->update( 'revision',
1901 [ 'rev_user_text' => $newUserName ],
1902 [ 'rev_id' => $record->getId() ] );
1903 $this->db
->update( 'user',
1904 [ 'user_name' => $newUserName ],
1905 [ 'user_id' => $record->getUser()->getId() ] );
1906 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) {
1907 $this->db
->update( 'actor',
1908 [ 'actor_name' => $newUserName ],
1909 [ 'actor_user' => $record->getUser()->getId() ] );
1912 // Reload the record, passing $fromCache as true to force fresh info from the db,
1913 // and regrab the user name
1914 $recordAfter = $store->newRevisionFromRow(
1915 $this->revisionToRow( $rev ),
1920 $userNameAfter = $recordAfter->getUser()->getName();
1922 // The two user names should be different.
1923 // If they are the same, we are seeing a cached value, which is bad.
1924 $this->assertNotSame( $userNameBefore, $userNameAfter );
1926 // This is implied by the above assertion, but explicitly check it, for completeness
1927 $this->assertSame( $newUserName, $userNameAfter );
1931 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1933 public function testNewRevisionFromRow_revDelete() {
1934 $page = $this->getTestPage();
1936 /** @var Revision $rev */
1937 $rev = $page->doEditContent(
1938 new WikitextContent( $text ),
1940 )->value
['revision'];
1942 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1943 $record = $store->newRevisionFromRow(
1944 $this->revisionToRow( $rev ),
1949 // Grab the deleted bitmask
1950 $deletedBefore = $record->getVisibility();
1952 // Change the deleted bitmask in the database
1953 $this->db
->update( 'revision',
1954 [ 'rev_deleted' => RevisionRecord
::DELETED_TEXT
],
1955 [ 'rev_id' => $record->getId() ] );
1957 // Reload the record, passing $fromCache as true to force fresh info from the db,
1958 // and regrab the deleted bitmask
1959 $recordAfter = $store->newRevisionFromRow(
1960 $this->revisionToRow( $rev ),
1965 $deletedAfter = $recordAfter->getVisibility();
1967 // The two deleted flags should be different, because we modified the database.
1968 $this->assertNotSame( $deletedBefore, $deletedAfter );
1970 // This is implied by the above assertion, but explicitly check it, for completeness
1971 $this->assertSame( RevisionRecord
::DELETED_TEXT
, $deletedAfter );